import { createRouter, createWebHistory } from 'vue-router'
import adminRoutes from './admin'

// 公共页面
const Login = () => import('@/views/login/Login.vue')
const NotFound = () => import('@/views/common/NotFound.vue')

// 普通用户页面
const Layout = () => import('@/views/layout/Layout.vue')
const Home = () => import('@/views/home/Home.vue')
const SubmitCustom = () => import('@/views/custom/SubmitCustom.vue')
const CustomList = () => import('@/views/custom/CustomList.vue')
const ClothingStyle = () => import('@/views/clothing/Style.vue')
const ClothingMaterial = () => import('@/views/clothing/Material.vue')
const ClothingAccessory = () => import('@/views/clothing/Accessory.vue')
const OrderInfo = () => import('@/views/order/OrderInfo.vue')
const AccountManagement = () => import('@/views/user/AccountManagement.vue')
const EnterpriseInfo = () => import('@/views/user/EnterpriseInfo.vue')
const AppointmentForm = () => import('@/views/appointment/AppointmentForm.vue')
const DesignerSelection = () => import('@/views/appointment/DesignerSelection.vue')
const ProductReview = () => import('@/views/review/ProductReview.vue')
const DesignerReview = () => import('@/views/review/DesignerReview.vue')

// 设计师页面
const DesignerLayout = () => import('@/views/designer/layout/DesignerLayout.vue')
const DesignerDashboard = () => import('@/views/designer/dashboard/DesignerDashboard.vue')
const TaskInfo = () => import('@/views/designer/task/TaskInfo.vue')
const TaskSchedule = () => import('@/views/designer/task/TaskSchedule.vue')
const OrderDataUpload = () => import('@/views/designer/order/OrderDataUpload.vue')
const AccountSettings = () => import('@/views/designer/profile/AccountSettings.vue')
const ProfileInfo = () => import('@/views/designer/profile/ProfileInfo.vue')
const ServiceFeedback = () => import('@/views/designer/profile/ServiceFeedback.vue')

// 管理员页面
const AdminLayout = () => import('@/views/admin/layout/AdminLayout.vue')
const AdminDashboard = () => import('@/views/admin/dashboard/AdminDashboard.vue')
const UserList = () => import('@/views/admin/user/UserList.vue')
const MaterialList = () => import('@/views/admin/material/MaterialList.vue')
const StyleList = () => import('@/views/admin/style/StyleList.vue')
const StatisticsReport = () => import('@/views/admin/statistics/StatisticsReport.vue')

const routes = [
  {
    path: '/',
    redirect: '/admin/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录', isPublic: true }
  },
  // 普通用户路由
  {
    path: '/',
    component: Layout,
    meta: { requiresAuth: true, role: 'user' },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: Home,
        meta: { title: '首页', icon: 'home' }
      },
      {
        path: 'custom/submit',
        name: 'SubmitCustom',
        component: SubmitCustom,
        meta: { title: '提交定制', icon: 'plus' }
      },
      {
        path: 'custom/list',
        name: 'CustomList',
        component: CustomList,
        meta: { title: '定制列表', icon: 'list' }
      },
      {
        path: 'clothing',
        name: 'ClothingCustomization',
        meta: { title: '服装定制管理', icon: 'scissors' },
        children: [
          {
            path: 'style',
            name: 'ClothingStyle',
            component: ClothingStyle,
            meta: { title: '服装款式', icon: 'clothes' }
          },
          {
            path: 'material',
            name: 'ClothingMaterial',
            component: ClothingMaterial,
            meta: { title: '服装材质', icon: 'fabric' }
          },
          {
            path: 'accessory',
            name: 'ClothingAccessory',
            component: ClothingAccessory,
            meta: { title: '服装配饰', icon: 'accessory' }
          }
        ]
      },
      {
        path: 'order',
        name: 'OrderCenter',
        meta: { title: '订单中心', icon: 'document' },
        children: [
          {
            path: 'info',
            name: 'OrderInfo',
            component: OrderInfo,
            meta: { title: '订单信息', icon: 'info' }
          }
        ]
      },
      {
        path: 'user',
        name: 'UserCenter',
        meta: { title: '个人中心', icon: 'user' },
        children: [
          {
            path: 'account',
            name: 'AccountManagement',
            component: AccountManagement,
            meta: { title: '账号管理', icon: 'setting' }
          },
          {
            path: 'enterprise',
            name: 'EnterpriseInfo',
            component: EnterpriseInfo,
            meta: { title: '企业信息管理', icon: 'office-building' }
          }
        ]
      },
      {
        path: 'appointment',
        name: 'Appointment',
        meta: { title: '上门服务预约', icon: 'calendar' },
        children: [
          {
            path: 'form',
            name: 'AppointmentForm',
            component: AppointmentForm,
            meta: { title: '预约信息填写', icon: 'document' }
          },
          {
            path: 'designer',
            name: 'DesignerSelection',
            component: DesignerSelection,
            meta: { title: '设计师选择', icon: 'user' }
          }
        ]
      },
      {
        path: 'review',
        name: 'Review',
        meta: { title: '服务评价', icon: 'star' },
        children: [
          {
            path: 'product',
            name: 'ProductReview',
            component: ProductReview,
            meta: { title: '产品评价', icon: 'goods' }
          },
          {
            path: 'designer',
            name: 'DesignerReview',
            component: DesignerReview,
            meta: { title: '设计师评价', icon: 'user' }
          }
        ]
      }
    ]
  },
  // 设计师路由
  {
    path: '/designer',
    component: DesignerLayout,
    meta: { requiresAuth: true, role: 'designer' },
    children: [
      {
        path: 'dashboard',
        name: 'DesignerDashboard',
        component: DesignerDashboard,
        meta: { title: '仪表盘', icon: 'dashboard' }
      },
      {
        path: 'task/info',
        name: 'TaskInfo',
        component: TaskInfo,
        meta: { title: '任务信息', icon: 'task' }
      },
      {
        path: 'task/schedule',
        name: 'TaskSchedule',
        component: TaskSchedule,
        meta: { title: '日程安排', icon: 'calendar' }
      },
      {
        path: 'order-data',
        name: 'OrderDataUpload',
        component: OrderDataUpload,
        meta: { title: '订单数据管理', icon: 'upload' }
      },
      {
        path: 'profile/account',
        name: 'AccountSettings',
        component: AccountSettings,
        meta: { title: '账号与密码', icon: 'setting' }
      },
      {
        path: 'profile/info',
        name: 'ProfileInfo',
        component: ProfileInfo,
        meta: { title: '个人信息管理', icon: 'user' }
      },
      {
        path: 'profile/feedback',
        name: 'ServiceFeedback',
        component: ServiceFeedback,
        meta: { title: '服务反馈', icon: 'chat' }
      }
    ]
  },
  // 管理员路由
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/dashboard/index.vue'),
        meta: { title: '仪表盘' }
      },
      {
        path: 'user',
        name: 'UserManagement',
        component: () => import('@/views/admin/user/UserManagement.vue'),
        meta: { title: '用户信息管理' }
      },
      {
        path: '/admin/clothing/style',
        name: 'ClothingStyleList',
        component: () => import('@/views/admin/clothing/style/StyleList.vue'),
        meta: { title: '服装款式管理' }
      },
      {
        path: '/admin/clothing/material',
        name: 'ClothingMaterialList',
        component: () => import('@/views/admin/clothing/material/MaterialList.vue'),
        meta: { title: '服装材质管理' }
      },
      {
        path: '/admin/clothing/accessory',
        name: 'ClothingAccessoryList',
        component: () => import('@/views/admin/clothing/accessory/AccessoryList.vue'),
        meta: { title: '服装配饰管理' }
      },
      {
        path: 'user/info',
        name: 'UserInfo',
        component: () => import('@/views/admin/user/UserInfo.vue'),
        meta: { title: '用户信息', icon: 'user' }
      },
      {
        path: 'material',
        name: 'MaterialList',
        component: MaterialList,
        meta: { title: '材料管理', icon: 'material' }
      },
      {
        path: 'style',
        name: 'StyleList',
        component: StyleList,
        meta: { title: '款式管理', icon: 'style' }
      },
      {
        path: 'statistics',
        name: 'StatisticsReport',
        component: StatisticsReport,
        meta: { title: '数据统计', icon: 'statistics' }
      },
      {
        path: 'order',
        component: () => import('@/views/admin/order/index'),
        name: 'OrderManagement',
        meta: { title: '订单管理', icon: 'el-icon-s-order' },
        children: [
          {
            path: 'list',
            component: () => import('@/views/admin/order/OrderList'),
            name: 'OrderList',
            meta: { title: '订单列表' }
          },
          {
            path: 'detail/:id',
            component: () => import('@/views/admin/order/OrderProcess'),
            name: 'OrderDetail',
            meta: { title: '订单详情', activeMenu: '/admin/order/list' },
            hidden: true
          },
          {
            path: 'analysis',
            component: () => import('@/views/admin/order/OrderAnalysis'),
            name: 'OrderAnalysis',
            meta: { title: '订单分析' }
          },
          {
            path: 'dispute',
            component: () => import('@/views/admin/order/DisputeList'),
            name: 'DisputeList',
            meta: { title: '纠纷处理' }
          },
          {
            path: 'dispute/:id',
            component: () => import('@/views/admin/order/DisputeDetail'),
            name: 'DisputeDetail',
            meta: { title: '纠纷详情', activeMenu: '/admin/order/dispute' },
            hidden: true
          }
        ]
      },
      {
        path: '/admin/designer',
        component: AdminLayout,
        redirect: '/admin/designer/management',
        name: 'DesignerAdmin',
        meta: { title: '设计师管理', icon: 'el-icon-s-custom', role: 'admin' },
        children: [
          {
            path: 'management',
            name: 'DesignerManagement',
            component: () => import('@/views/admin/designer/DesignerManagement'),
            meta: { title: '资质审核与信息维护', icon: 'el-icon-user', role: 'admin' }
          },
          {
            path: 'schedule',
            name: 'DesignerSchedule',
            component: () => import('@/views/admin/designer/DesignerManagement'),
            meta: { title: '日程管理', icon: 'el-icon-date', role: 'admin' }
          },
          {
            path: 'review',
            name: 'DesignerReview',
            component: () => import('@/views/admin/designer/DesignerManagement'),
            meta: { title: '服务质量监控与评价', icon: 'el-icon-star-on', role: 'admin' }
          }
        ]
      }
    ]
  },
  ...adminRoutes
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 服装定制管理系统` : '服装定制管理系统'
  
  // 判断该路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const token = localStorage.getItem('token')
    const userRole = localStorage.getItem('userRole')
    
    if (!token) {
      // 没有token，重定向到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 检查角色是否匹配
      const requireRole = to.matched.find(record => record.meta.role)?.meta.role
      
      if (requireRole && requireRole !== userRole) {
        // 角色不匹配，重定向到对应角色的默认页面
        if (userRole === 'user') {
          next('/home')
        } else if (userRole === 'designer') {
          next('/designer/dashboard')
        } else if (userRole === 'admin') {
          next('/admin/dashboard')
        } else {
          next('/login')
        }
      } else {
        next()
      }
    }
  } else {
    // 不需要登录权限的路由
    next()
  }
})

export default router 